﻿using SVTTranslateTextEngine.Core;
using SVTTranslateTextEngine.Properties;
using SVTTranslateTextEngine.TTInputParams;

namespace SVTTranslateTextEngine.TTChecker
{
    /// <summary>
    /// Achieve validation logic of file transfer in the translation process .
    /// </summary>
    public class FileChecker
    {
        public const string ERR_FILE_TRANSFER_NOT_ALLOWED = "Input by file transfer model is not allowed ! \r\n",
                            ERR_FILE_TOO_LARGE = "Input file is too larger to translate ! \r\n",
                            ERR_FILE_NOT_SUPPORTED = "Input file type is not supported! \r\n";

        /// <summary>
        /// Check the file can be translate , or not ?
        /// It will call these methods by the step 
        /// 1.check_file_size
        /// 2.check_file_type
        /// and more steps
        /// </summary>
        /// <returns></returns>
        public string CheckFileIsCanTranslate(FileInputParam input)
        {
            string errorMessage = "";
            if (!check_file_transfer_allowed())
                errorMessage = ERR_FILE_TRANSFER_NOT_ALLOWED;
            if (!check_file_size(input.szFromFilename))
                errorMessage = ERR_FILE_TOO_LARGE;
            if (!check_file_type(input.szFromFilename))
                errorMessage = ERR_FILE_NOT_SUPPORTED;
            return errorMessage;
        }

        /// <summary>
        /// check the translate can be transfered by file , or not ?
        /// </summary>
        /// <returns>true or false </returns>
        private static bool check_file_transfer_allowed()
        {
            bool isAllowed = true;
            if (Settings.Default.TranslateFileToFile.ToUpper() != "Y")
            {
                isAllowed = false;
            }
            return isAllowed;
        }

        /// <summary>
        /// check the context file size is over the max length , or not ?
        /// </summary>
        /// <param name="szFileFullPath"></param>
        /// <returns>true or false </returns>
        private bool check_file_size(string szFileFullPath)
        {
            return new FileAccessHelper().GetTheFileSize(szFileFullPath) <= Settings.Default.FileToFileMaxLength;
        }

        /// <summary>
        /// check the file type is supported .
        /// </summary>
        /// <param name="szFileFullPath">the file path which be translated .</param>
        /// <returns>true or false </returns>
        private bool check_file_type(string szFileFullPath)
        {
            return szFileFullPath.ToLower().EndsWith(".txt");
        }
    }
}